package com.canlihao.ui.mvp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.canlihao.ui.R;
import com.canlihao.ui.base.BaseActivity;
import com.canlihao.ui.base.BaseWhiteTopbarActivity;
import com.canlihao.ui.mvp.SharedPreferencesUtils;
import com.canlihao.ui.mvp.http.CommonConstant;
import com.canlihao.ui.mvp.presenter.LoginPresenter;
import com.canlihao.ui.mvp.view.LoginView;
import com.canlihao.ui.utils.MyLog;
import com.canlihao.ui.utils.StatusBarUtil;
import com.canlihao.ui.utils.ToastUtil;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/*******
 *
 *Created by panda李
 *
 *创建时间 2020/3/30
 *
 *类描述：
 *
 ********/
public class LoginActivityMvp extends BaseActivity implements LoginView {
    private static final String TAG = "LoginActivityMvp";
    @BindView(R.id.edit_username)
    EditText username;
    @BindView(R.id.edit_password)
    EditText password;
    @BindView(R.id.checkbox_remember_password)
    CheckBox rememberPassword;
    @BindView(R.id.checkbox_auto_login)
    CheckBox autoLogin;
    @BindView(R.id.button_login)
    Button buttonLogin;
    @BindView(R.id.loginPanel)
    LinearLayout loginPanel;
    @BindView(R.id.button_register)
    Button buttonRegister;
    @BindView(R.id.button_forget_password)
    Button buttonForgetPassword;
    protected QMUITopBarLayout mTopBar;
    LoginPresenter loginPresenter;
    private int roleChoice = 0;
    @Override
    protected void initView() {
//        StatusBarUtil.setColor(this, getResources().getColor(R.color.person_real_title), 0);
        mTopBar = findViewById(R.id.topbar);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
        mTopBar.setVisibility(View.GONE);
        SharedPreferencesUtils.getInstance(getContext(), "LOGIN");
        loginPresenter = new LoginPresenter();
        loginPresenter.attachView(this);
        readSharedPreferences();
    }

    @OnCheckedChanged(R.id.checkbox_remember_password)
    public void rememberPassword(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            Log.d(TAG, "记住密码已选中");
            showToast("记住密码已选中");
            SharedPreferencesUtils.putData("REMEMBER_PASSWORD", true);
        } else {
            Log.d(TAG, "记住密码没有选中");
            showToast("记住密码没有选中");
            SharedPreferencesUtils.putData("REMEMBER_PASSWORD", false);
        }
    }
    @OnCheckedChanged(R.id.checkbox_auto_login)
    public void autoLogin(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            Log.d(TAG, "自动登录已选中");
            showToast("自动登录已选中");
            SharedPreferencesUtils.putData("AUTO_LOGIN", true);
        } else {
            Log.d(TAG, "自动登录未选中");
            showToast("自动登录未选中");
            SharedPreferencesUtils.putData("AUTO_LOGIN", false);
        }
    }
    @OnClick(R.id.button_login)
    public void login(View view) {
        loginPresenter.login(username.getText().toString().trim(), password.getText().toString().trim());
    }
    @OnClick(R.id.button_register)
    public void register(View view) {
        Intent intent = new Intent();
    }
    @OnClick(R.id.button_forget_password)
    public void forgetPassword(View view) {
        Intent intent = new Intent();
    }
    @Override
    public void showData(JSONObject successData) {
        MyLog.i("李魏",successData.toString());
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(successData.getString("message"));
                SharedPreferencesUtils.putData(CommonConstant.USERNAME, username.getText().toString().trim());
                SharedPreferencesUtils.putData(CommonConstant.PASSWORD, password.getText().toString().trim());
                Intent intent = new Intent();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000);
                            Looper.prepare();
                            showToast(successData.toString());
                            finish();
                            Looper.loop();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }
    @Override
    public int getLayoutResId() {
        return R.layout.login_mvp_activity;
    }
    @Override
    public void showToast(String msg) {
        ToastUtil.showToast(getContext(),msg);
    }

    @Override
    public void showFailure(JSONObject errorData) {
        ToastUtil.showToast(getContext(), errorData.getString("message"));
    }

    @Override
    public void showErr() {

    }

    @Override
    public Context getContext() {
        return this;
    }
    @Override
    public void readSharedPreferences() {
        //记住密码
        if (SharedPreferencesUtils.getData(CommonConstant.REMEMBER_PASSWORD, false).equals(true)) {
            username.setText((String) SharedPreferencesUtils.getData(CommonConstant.USERNAME, ""));
            password.setText((String) SharedPreferencesUtils.getData(CommonConstant.PASSWORD, ""));
        }
        //自动登录
        if (SharedPreferencesUtils.getData(CommonConstant.AUTO_LOGIN, false).equals(true)) {
            loginPresenter.login(username.getText().toString().trim(), password.getText().toString().trim());
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        loginPresenter.detachView();
    }
}
